#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <arpa/inet.h>
#include <time.h>

#define PORT 8080
#define BUFFER_SIZE 1024

typedef struct {
    int client_socket;
} client_data;

// I/O操作计时
typedef struct {
    double socket_read_time;
    double socket_write_time;
    double file_read_time;
    double log_write_time;
} io_time;

pthread_mutex_t lock;
io_time total_time = {0};
int request_count = 0;

// 计时函数
double get_time_diff(struct timespec start, struct timespec end) {
    return (end.tv_sec - start.tv_sec) * 1000.0 + (end.tv_nsec - start.tv_nsec) / 1000000.0;
}

void* handle_client(void* arg) {
    client_data* data = (client_data*)arg;
    int client_socket = data->client_socket;
    free(data);

    char request[BUFFER_SIZE];
    char response[BUFFER_SIZE];
    struct timespec start, end;

    // 计时：socket读取
    clock_gettime(CLOCK_MONOTONIC, &start);
    int n = read(client_socket, request, BUFFER_SIZE - 1);
    clock_gettime(CLOCK_MONOTONIC, &end);
    double socket_read_time = get_time_diff(start, end);
    if (n > 0) {
        request[n] = '\0';
        printf("Client request: %s\n", request);

        // 简单的HTTP响应
        strcpy(response, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
        strcat(response, "<html><body><h1>Hello, World!</h1></body></html>");

        // 计时：socket发送
        clock_gettime(CLOCK_MONOTONIC, &start);
        write(client_socket, response, strlen(response));
        clock_gettime(CLOCK_MONOTONIC, &end);
        double socket_write_time = get_time_diff(start, end);

        // 记录I/O操作时间
        pthread_mutex_lock(&lock);
        total_time.socket_read_time += socket_read_time;
        total_time.socket_write_time += socket_write_time;
        request_count++;
        pthread_mutex_unlock(&lock);
    }

    close(client_socket);
    return NULL;
}

int main() {
    int server_socket, client_socket;
    struct sockaddr_in server_addr, client_addr;
    socklen_t client_len = sizeof(client_addr);
    pthread_t thread;

    // 初始化互斥锁
    pthread_mutex_init(&lock, NULL);

    // 创建套接字
    server_socket = socket(AF_INET, SOCK_STREAM, 0);
    if (server_socket < 0) {
        perror("Socket creation failed");
        exit(EXIT_FAILURE);
    }

    // 设置服务器地址
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = INADDR_ANY;
    server_addr.sin_port = htons(PORT);

    // 绑定套接字
    if (bind(server_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
        perror("Bind failed");
        close(server_socket);
        exit(EXIT_FAILURE);
    }

    // 开始监听
    if (listen(server_socket, 5) < 0) {
        perror("Listen failed");
        close(server_socket);
        exit(EXIT_FAILURE);
    }

    printf("Server is running on port %d...\n", PORT);

    while (1) {
        // 接受客户端连接
        client_socket = accept(server_socket, (struct sockaddr*)&client_addr, &client_len);
        if (client_socket < 0) {
            perror("Accept failed");
            continue;
        }

        // 创建线程处理客户端请求
        client_data* data = (client_data*)malloc(sizeof(client_data));
        data->client_socket = client_socket;
        if (pthread_create(&thread, NULL, handle_client, data) != 0) {
            perror("Thread creation failed");
            close(client_socket);
        }
    }

    // 关闭服务器套接字
    close(server_socket);

    // 打印I/O操作的平均时间
    printf("Average I/O times:\n");
    printf("Socket Read: %.2f ms\n", total_time.socket_read_time / request_count);
    printf("Socket Write: %.2f ms\n", total_time.socket_write_time / request_count);

    return 0;
}